package com.woniuxy.kaer.shop.consumer;


import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.shop.mapper.ProductOrderMapper;
import com.woniuxy.kaer.shop.model.ProductOrder;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class EvaluateOrderConsumer {

    @Resource
    private ProductOrderMapper productOrderMapper;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue("evaluate_queue"),
                    exchange = @Exchange("evaluate_order_ex"),
                    key = "evaluate_order"
            )
    )
    @RabbitHandler
    public void confirmPay(String productOrderId) {
        ProductOrder productOrder = productOrderMapper.selectById(productOrderId);
        //订单物流状态待评价
        System.out.println("自动好评");
        if (productOrder == null) {
            throw new RuntimeException("订单不存在");
        }
        System.out.println(productOrder.getOrderState().equals(Data.ORDER_STATE_EVALUATE));
        if (productOrder.getOrderState()==Data.ORDER_STATE_EVALUATE) {
            //订单还未评价，自动好评

            productOrder.setEvalute(5);
            productOrder.setOrderState(Data.ORDER_STATE_FINISH);
            productOrderMapper.updateById(productOrder);
            System.out.println("自动好评成功");
        }

    }
}
